package tool.workflow.fsSoft

import akka.stream.Materializer
import implicits.Implicits._
import play.api.libs.ws.WSClient
import shared.Pojo._
import shared.SharedTool
import tool.Pojo._
import shared.Pojo._
import tool.plot._
import tool.softT._
import tool.workflow.fsSoft.AllFsSoft.FSSoftBT
import tool.workflow.soft.AllSubSoft
import tool.{FeatureSelectionTool, Tool}
import utils.Utils

import java.io.File
import scala.collection.immutable.SeqMap
import scala.language.postfixOps
import dao._
import play.api.i18n.Messages
import shared.tool.DefaultPickle

/**
 * Created by yz on 3/12/2020
 */
object FSPreprocess extends FSSoftBT with AllSoftBT with PreprocessSoftT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit matrixFileDataDao: MatrixFileDataDao, messages: Messages): List[FileInfoData] = {
    val data = FeatureSelectionTool.getFsData(workspaceDir)
    val files = new File(workspaceDir, "preprocess_0").listFiles().filter { x =>
      x.getName.matches("^\\d+.*$") && !x.getName.endsWith(".txt")
    }
    val fileInfos = data.preprocess.datas.map(_.kind).zipWithIndex.map { case (kind, i) =>
      val curSoft = AllSubSoft.kindSoftMap(kind)
      val file = files.find(_.getName.startsWith(s"${i + 1}.")).get
      FileInfoData(file.getName, messages(curSoft.nameStr))
    }
    fileInfos :+ FileInfoData("Diff_Metabolite.csv", messages("diffMetabolite")) :+
      FileInfoData("Statistics.csv", "Statistics")
  }

  override def produceInput(workspaceDir: File, resultDir: File, index: Int, parentNodeStr: String, id: String)(
    implicit dao: MyDao,messages: Messages): Unit = {
    val groupFile = new File(workspaceDir.getParentFile, "group.txt")
    groupFile.fileCopyToDir(workspaceDir)
    FeatureSelectionTool.produceSubInputFile(workspaceDir, parentNodeStr)
    val data = FeatureSelectionTool.getFsData(workspaceDir.getParentFile)
    val calculateConfigJsonFile = Tool.getCalculateConfigJsonFile(workspaceDir)
    DefaultPickle.write(data.preprocess).toFile(calculateConfigJsonFile)
    val matrixFile = Tool.getMatrixFile(workspaceDir.getParentFile)
    val rawDataFile = new File(workspaceDir, "0.result.txt")
    matrixFile.copyTo(rawDataFile)
    val colorData = NaIdentifyColorData(data.preprocess.datas.map(_.naColor))
    Utils.t2ConfigFile(colorData, Tool.getColorJsonFile(workspaceDir))
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val groupFile = new File(workspaceDir, "group.txt")
    val data = FeatureSelectionTool.getFsData(workspaceDir.getParentFile)
    val commands = data.preprocess.datas.map(_.kind).zipWithIndex.map { kindI =>
      val (kind, i) = kindI
      val curSoft = AllSubSoft.kindSoftMap(kind)
      val si = s"${i+1}"
      curSoft.getCommand(si)
    }
    val i = data.preprocess.datas.size
    val splitCommand =
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "stat_calculate.R").unixPath} --index ${i}
         |${Tool.rScript} ${new File(Tool.rPath, "train_test_split.R").unixPath} --index ${i}
         |cp train_group.txt ../group.txt
         |cp train.txt ../matrix.txt
         |""".stripMargin
    val finalCommands = commands :+ splitCommand
    List(CommandData(workspaceDir, finalCommands))
  }

  override def resultDeal(workspaceDir: File, resultDir: File, index: Int)(implicit ws: WSClient,
                                                                           materializer: Materializer, messages: Messages): Unit = {
    val data = FeatureSelectionTool.getFsData(workspaceDir.getParentFile)
    val commands = data.preprocess.datas.map(_.kind).zipWithIndex.map { kindI =>
      val (kind, i) = kindI
      val curSoft = AllSubSoft.kindSoftMap(kind)
      val si =s"${i+1}"
      curSoft.execAfterCommand(workspaceDir, resultDir, si)
    }
    val i = data.preprocess.datas.size
    val file = new File(workspaceDir, s"${i}.result.txt")
    val finalDiffMetabolites = file.txtLines.drop(1).map(_.take(1))
    (List("Metabolite") :: finalDiffMetabolites).toFile(Tool.getDiffMetaboliteFile(workspaceDir))
    file.copyTo(Tool.getMatrixFile(workspaceDir.getParentFile))
  }

  override def resultCopy(workspaceDir: File, resultDir: File, index: Int)(implicit ws: WSClient,
                                                                           materializer: Materializer, messages: Messages): Unit = {
    super.resultCopy(workspaceDir, resultDir, index)
    val kind = SharedTool.getKind(workspaceDir.getName)
    val outDir = messages(kind)
    resultDir.myListFiles.filter(_.isDirectory).foreach { x =>
      x.renameTo(new File(resultDir, x.getName.replaceAll(kind, outDir)))
    }
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
  }

}
